<?php
/**
 * 体检报告
 *
 * User: kaikaic
 * Date: 2018/12/2
 * Time: 15:30
 */

namespace app\exam\model;

use app\exam\service\MecItemService;
use app\exam\service\MecService;
use app\exam\service\MecSetService;
use app\exam\service\ReportService;
use service\ExcelService;

class SetModel {

    /**
     * 读取EXCEL文件数据
     */
    public function readExcelFile($excelFile) {
        set_time_limit(0);
        $excelFile = env('root_path') . $excelFile;
        $excelData = ExcelService::importExcel($excelFile);
        if (!$excelData) {
            return 0;
        }

        $importCount = 0;
        for ($i=0; $i<count($excelData); $i++) {
            $tmp = $excelData[$i];

            $params = array();
            $params['mec_name'] = trim($tmp[0]);
            $params['unique_code'] = trim($tmp[1]);
            $params['name'] = trim($tmp[2]);
            $set_type= trim($tmp[3]);
            if($set_type=='男'){
                $params['set_type'] = 16;
            }else if($set_type=='女'){
                $params['set_type'] = 17;
            }else if($set_type=='长辈'){
                $params['set_type'] = 15;
            }
            $params['intro'] = trim($tmp[4]);
            $params['item'] = trim($tmp[5]);
            $params['old_price'] = round(trim($tmp[6])/0.9);
            $params['price'] = trim($tmp[6]);
            // 数据记录
            $rs = $this->doImport($params);
            if ($rs) {
                $importCount++;
            }
        }

        return $importCount;
    }

    /**
     * 导入实现
     *
     * @param $data
     * @return bool
     */
    private function doImport($data) {
//        $data = $this->excelDataFilter($data);
//        if (!$data) {
//            return false;
//        }
        $condition['unique_code'] = $data['unique_code'];
        $seinfo =  MecSetService::getInstance()->single($condition);
        if($seinfo){
            $params = array();
            $params['name'] = $data['name'];
            $params['unique_code'] = $data['unique_code'];
            $params['intro'] = $data['intro'];
            $params['old_price'] = $data['old_price'];
            $params['price'] = $data['price'];
            $params['set_type'] = $data['set_type'];

            MecSetService::getInstance()->update($params,$data['unique_code'],'unique_code');
        }else{

        // 医院是否存在
        $mec_name = $data['mec_name'];
        $mec = MecService::getInstance()->single(array('name'=>$mec_name));
        if (!$mec) {
            return false;
        }
        // 项目是否存在
        $exam_item_name = '默认分类';
        $exam_item_class = MecItemService::getInstance()->single(array('name'=>$exam_item_name,'mec_id'=>$mec['id']));
        if (!$exam_item_class) {
            $query['mec_id'] = $mec['id'];
            $query['name'] ='默认分类';
            $query['is_default'] = 1;
            $query['sort'] =1;
            $query['status'] = 1;
            $query['create_time'] = time();
            $itemclassid  = MecItemService::getInstance()->insert($query);
        }else{
            $itemclassid  = $exam_item_class['id'];
        }
        $itemall =  $data['item'];
        $itemArr = explode(",",$itemall);
        $itemAll = array();
        for ($i=0;$i<count($itemArr);$i++){
           $itemres =  MecItemService::getInstance()->single(array('name'=>$itemArr[$i],'mec_id'=>$mec['id'],'item_set_id'=>$itemclassid),1);
            if($itemres){
                $itemid = $itemres['id'];
            }else{
                $itemaddres['name'] = $itemArr[$i];
                $itemaddres['mec_id'] = $mec['id'];
                $itemaddres['item_set_id'] = $itemclassid;
                $itemaddres['create_time'] = time();
                $itemid  = MecItemService::getInstance()->insert($itemaddres,1);
            }
            array_push($itemAll,$itemid);
        }
        $params = array();
        $params['name'] = $data['name'];
        $params['mec_id'] = $mec['id'];
        $params['unique_code'] = $data['unique_code'];
        $params['item_ids'] = serialize($itemAll);
        $params['intro'] = $data['intro'];
        $params['status'] = 1;
        $params['sort'] = MecSetService::getInstance()->getMaxSort() + 1;
        $params['create_time'] = time();
        $params['old_price'] = $data['old_price'];
        $params['price'] = $data['price'];
        $params['set_type'] = $data['set_type'];

        return MecSetService::getInstance()->insert($params);
        }

    }

    /**
     * 对excel数据过滤
     */
    private function excelDataFilter($data) {
        if (!$data['mec_name'] || !$data['exam_item_class'] || !$data['name']) {
            return array();
        }
        return $data;
    }
}

